home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6504 < prev    next >
Encoding:
Text File  |  1996-08-05  |  10.6 KB  |  330 lines

  1. Path: Belgium.EU.net!box!pahint
  2. From: pahint@eunet.be (Pieter Hintjens)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Universal include file
  5. Date: 25 Feb 1996 13:25:36 GMT
  6. Organization: EUnet Belgium, Leuven, Belgium
  7. Message-ID: <4gpo0g$hve@news.Belgium.EU.net>
  8. References: <4gnjea$30m@news.Belgium.EU.net> <4gpfh5$fmo@news.Belgium.EU.net>
  9. NNTP-Posting-Host: box.eunet.be
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. This is version 1.01 of the Universal Include File..  Ta-taaa...!
  13. Changes from version 1.00:
  14.    - __UTYPE_xxx symbol for UNIX version
  15.    - removed some of the more personal macros (AND, OR, strlast,...)
  16.    - byte = 8 bits, dbyte = 16 bits, qbyte = 32 bits
  17.  
  18. --
  19. Pieter A. Hintjens
  20.  
  21.  
  22. /*===========================================================================*
  23.  *                                                                           *
  24.  *  prelude.h   Universal header file for C programming                      *
  25.  *                                                                           *
  26.  *  Written:    93/03/29  Pieter Hintjens  <ph@mymail.com>                   *
  27.  *  Revised:    96/02/25  Version 1.01                                       *
  28.  *                                                                           *
  29.  *  This header encapsulates all generally-useful include files and defines  *
  30.  *  various useful things.  To use, specify as first include file in code.   *
  31.  *                                                                           *
  32.  *  Contributors & critics:                                                  *
  33.  *      Gordon Burditt    <gordon@sneaky.lerctr.org>      25 Feb 1996        *
  34.  *      Steve Leibel      <stevel@coastside.net>          25 Feb 1996        *
  35.  *      Guus Leeuw jr.    <guusl@eiffel.com>              25 Feb 1996        *
  36.  *                                                                           *
  37.  *  Copyright (c) 1991-96 Pieter A. Hintjens.  May be freely distributed.    *
  38.  *===========================================================================*/
  39.  
  40. #ifndef _PRELUDE_INCLUDED               /*  Allow multiple inclusions        */
  41. #define _PRELUDE_INCLUDED
  42.  
  43.  
  44. /*- Generally-useful include files ------------------------------------------*/
  45.  
  46. #include <ctype.h>
  47. #include <limits.h>
  48. #include <stdarg.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <time.h>
  53. #include <signal.h>
  54. #include <errno.h>
  55. #include <fcntl.h>
  56.  
  57.  
  58. /*- Establish the compiler and computer system ------------------------------*/
  59. /*
  60.  *  Defines one or more of these symbols, for use in any non-portable
  61.  *  code:
  62.  *
  63.  *  __WINDOWS__         Microsoft C/C++ with Windows calls
  64.  *  __MSDOS__           File system is MS-DOS
  65.  *  __VMS__             System is VAX/VMS or Alpha/OpenVMS
  66.  *  __UNIX__            System is UNIX
  67.  *  __MAC__             System is Mac/OS
  68.  *
  69.  *  __32BIT__           OS/compiler is 32 bits
  70.  *  __64BIT__           OS/compiler is 64 bits
  71.  *
  72.  *  When __UNIX__ is defined, we also define exactly one of these:
  73.  *
  74.  *  __UTYPE_AUX         Apple AUX
  75.  *  __UTYPE_DECALPHA    Digital UNIX (Alpha)
  76.  *  __UTYPE_HPUX        HP/UX
  77.  *  __UTYPE_IBMAIX      IBM RS/6000 AIX
  78.  *  __UTYPE_LINUX       Linux
  79.  *  __UTYPE_MIPS        MIPS (BSD 4.3/System V mixture)
  80.  *  __UTYPE_NETBSD      NetBSD
  81.  *  __UTYPE_NEXT        NeXT
  82.  *  __UTYPE_SCO         SCO Unix
  83.  *  __UTYPE_SUNOS       SunOS
  84.  *  __UTYPE_SUNSOLARIS  Sun Solaris
  85.  *  __UTYPE_GENERIC     Any other UNIX
  86.  */
  87.  
  88. #define __32BIT__                       /*  Assume 32-bit OS/compiler        */
  89.  
  90. #if (defined (WIN32) || defined (WINDOWS))
  91. #   undef __WINDOWS__
  92. #   define __WINDOWS__
  93. #   undef __MSDOS__
  94. #   define __MSDOS__
  95. #endif
  96.  
  97. /*  MSDOS      is Microsoft C                                                */
  98. /*  _MSC_VER   is Microsoft C                                                */
  99. /*  __TURBOC__ is Borland Turbo C                                            */
  100. #if (defined (MSDOS) || defined (_MSC_VER) || defined (__TURBOC__))
  101. #   undef __MSDOS__
  102. #   define __MSDOS__
  103. #endif
  104.  
  105. /*  VMS        is VAX C (VAX/VMS)                                            */
  106. /*  __VMS      is Dec C (Alpha/OpenVMS)                                      */
  107. /*  __vax__    is gcc                                                        */
  108. #if (defined (VMS) || defined (__VMS) || defined (__vax__))
  109. #   undef __VMS__
  110. #   define __VMS__
  111. #endif
  112.  
  113. /*  unix          is SunOS                                                   */
  114. /*  __unix__      is gcc                                                     */
  115. /*  _POSIX_SOURCE is various UNIX systems, maybe also VAX/VMS                */
  116. #if (defined (unix) || defined (__unix__) || defined (_POSIX_SOURCE))
  117. #   if (!defined (__VMS__))
  118. #       undef __UNIX__
  119. #       define __UNIX__
  120. #       if (defined (__alpha))          /*  Digital UNIX is 64-bit           */
  121. #           undef  __32BIT__
  122. #           define __64BIT__
  123. #       endif
  124. #   endif
  125. #endif
  126.  
  127. /*  Try to define a __UTYPE_xxx symbol...                                    */
  128. #if (defined __UNIX__)
  129. #   if (defined (_AUX))
  130. #       define __UTYPE_AUX
  131. #   elif (defined (__alpha))
  132. #       define __UTYPE_DECALPHA
  133. #   elif (defined (__hpux))
  134. #       define __UTYPE_HPUX
  135. #   elif (defined (_AIX) || defined (AIX))
  136. #       define __UTYPE_IBMAIX
  137. #   elif (defined (linux))
  138. #       define __UTYPE_LINUX
  139. #   elif (defined (Mips))
  140. #       define __UTYPE_MIPS
  141. #   elif (defined (NetBSD))
  142. #       define __UTYPE_NETBSD
  143. #   elif (defined (NeXT))
  144. #       define __UTYPE_NEXT
  145. #   elif (defined (sco))
  146. #       define __UTYPE_SCO
  147. #   elif (defined (SUNOS))
  148. #       define __UTYPE_SUNOS
  149. #   elif (defined (SOLARIS))
  150. #       define __UTYPE_SUNSOLARIS
  151. #   else
  152. #       define __UTYPE_GENERIC
  153. #   endif
  154. #endif
  155.  
  156. /*  Untested                                                                 */
  157. #if (defined (_MAC))
  158. #   undef __MAC__
  159. #   define __MAC__
  160. #endif
  161.  
  162.  
  163. /*- System-specific include files -------------------------------------------*/
  164.  
  165. #if (defined (__TURBOC__))              /*  Borland Turbo-C uses this        */
  166. #   include <alloc.h>                   /*    name for its include file      */
  167. #else
  168. #   include <malloc.h>
  169. #endif
  170.  
  171. #if (defined (__WINDOWS__))             /*  When __WINDOWS__ is defined,     */
  172. #   include <windows.h>                 /*    so is __MSDOS__                */
  173. #endif
  174.  
  175. #if (defined (__MSDOS__))
  176. #   include <dos.h>
  177. #   include <io.h>
  178. #   include <fcntl.h>
  179. #   include <sys\types.h>
  180. #   include <sys\stat.h>
  181. #endif
  182.  
  183. #if (defined (__UNIX__))
  184. #   include <netdb.h>
  185. #   include <unistd.h>
  186. #   include <netinet/in.h>
  187. #   include <sys/types.h>
  188. #   include <sys/param.h>
  189. #   include <sys/socket.h>
  190. #   include <sys/time.h>
  191. #   include <sys/stat.h>
  192.     /*  Specific includes for UNIX varieties                                 */
  193. #   if defined (__UTYPE_IBMAIX)
  194. #       include <sys/select.h>
  195. #   endif
  196. #endif
  197.  
  198. #if (defined (__VMS__))
  199. #   include <types.h>
  200. #   include <netdb.h>
  201. #   include <unixio.h>
  202. #   include <in.h>
  203. #   include <param.h>
  204. #   include <socket.h>
  205. #   include <time.h>
  206. #   include <stat.h>
  207. #endif
  208.  
  209.  
  210. /*- Data types --------------------------------------------------------------*/
  211.  
  212. typedef int            Bool;            /*  Boolean TRUE / FALSE value       */
  213. typedef unsigned char  byte;            /*  Single unsigned byte = 8 bits    */
  214. typedef unsigned short dbyte;           /*  Double byte = 16 bits            */
  215. #if (defined (__32BIT__))
  216. typedef unsigned long  qbyte;           /*  Quad byte = 32 bits              */
  217. #else
  218. typedef unsigned int   qbyte;           /*  Quad byte = 32 bits              */
  219. #endif
  220. typedef void (*function) (void);        /*  Address of simple function       */
  221. #define local static void               /*  Shorthand for local functions    */
  222.  
  223.  
  224. /*- Check compiler data type sizes ------------------------------------------*/
  225.  
  226. #if (UCHAR_MAX != 0xFF)
  227. #   error "Cannot compile: must change definition of 'byte'."
  228. #endif
  229. #if (USHRT_MAX != 0xFFFFU)
  230. #   error "Cannot compile: must change definition of 'dbyte'."
  231. #endif
  232. #if (defined (__32BIT__))
  233. #   if (ULONG_MAX != 0xFFFFFFFFUL)
  234. #       error "Cannot compile: must change definition of 'qbyte'."
  235. #   endif
  236. #else
  237. #   if (UINT_MAX != 0xFFFFFFFFU)
  238. #       error "Cannot compile: must change definition of 'qbyte'."
  239. #   endif
  240. #endif
  241.  
  242.  
  243. /*- Pseudo-functions --------------------------------------------------------*/
  244.  
  245. #define until(expr)     while (!(expr))     /*  do { ... } until (expr)      */
  246. #define streq(s1, s2)   (!strcmp ((s1), (s2)))
  247. #define strneq(s1, s2)  (strcmp ((s1), (s2)))
  248. #define strused(s)      (*(s) != 0)
  249. #define strnull(s)      (*(s) == 0)
  250. #define strclr(s)       (*(s) = 0)
  251.  
  252. #define bit_msk(bit)    (1 << (bit))
  253. #define bit_set(x, bit) ((x) |=  bit_msk (bit))
  254. #define bit_clr(x, bit) ((x) &= ~bit_msk (bit))
  255. #define bit_tst(x, bit) ((x) &   bit_msk (bit))
  256.  
  257. #define tblsize(x)      (sizeof (x) / sizeof ((x) [0]))
  258. #define tbllast(x)      (x [tblsize (x) - 1]);
  259.  
  260. #if (defined (random))
  261. #   undef random
  262. #   undef randomize
  263. #endif
  264. #if (defined (min))
  265. #   undef min
  266. #   undef max
  267. #endif
  268.  
  269. #if (defined (__32BIT__))
  270. #define random(num)     (int) ((long) rand () % (num))
  271. #else
  272. #define random(num)     (int) ((int)  rand () % (num))
  273. #endif
  274. #define randomize()     srand ((unsigned) time (NULL))
  275. #define min(a,b)        (((a) < (b))? (a): (b))
  276. #define max(a,b)        (((a) > (b))? (a): (b))
  277.  
  278.  
  279. /*- ASSERT and debugging ----------------------------------------------------*/
  280.  
  281. #if (defined (DEBUG))
  282.     /*  Define _Assert function here locally                                 */
  283.     local _Assert (char *File, unsigned Line)
  284.     {
  285.         fflush  (stdout);
  286.         fprintf (stderr, "\nAssertion failed: %s, line %u\n", File, Line);
  287.         fflush  (stderr);
  288.         abort   ();
  289.     }
  290. #   define ASSERT(f)            \
  291.         if (f)                  \
  292.             ;                   \
  293.         else                    \
  294.             _Assert (__FILE__, __LINE__)
  295. #else
  296. #   define ASSERT(f)
  297. #endif
  298.  
  299.  
  300. /*- Boolean operators and constants -----------------------------------------*/
  301.  
  302. #if (defined (TRUE))
  303. #   undef TRUE
  304. #   undef FALSE
  305. #endif
  306. #define TRUE           (1==1)
  307. #define FALSE          (1==0)
  308.  
  309.  
  310. /*- Symbolic constants ------------------------------------------------------*/
  311.  
  312. #define FORK_ERROR      -1              /*  Return codes from fork()         */
  313. #define FORK_CHILD      0
  314. #if (!defined (LINE_MAX))
  315. #   define LINE_MAX     255
  316. #endif
  317.  
  318.  
  319. /*- System-specific definitions ---------------------------------------------*/
  320.  
  321. #if (defined (__VMS__))
  322. #   define RET_OKAY             1       /*  Return codes for main()          */
  323. #   define RET_ERROR            0       /*  VMS does it its own way.         */
  324. #else
  325. #   define RET_OKAY             0       /*  On non-VMS systems 0 is okay,    */
  326. #   define RET_ERROR            1       /*    and 1 is an error.             */
  327. #endif
  328.  
  329. #endif                                  /*  Include PRELUDE.H                */
  330.